<template>
  <div class="settin">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理" />
        <el-tab-pane label="公司信息" />
        <el-button
          type="primary"
          size="mini"
          round
          class="btn"
          @click="addClick"
        >+新增角色</el-button>
        <el-table border :data="tableData">
          <el-table-column label="序号" width="80%">
            <template v-slot="{ $index }">
              <div>
                {{ (page.page - 1) * page.pagesize + $index + 1 }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="角色" prop="name" sortable />
          <el-table-column label="描述" prop="description" sortable />
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <div>
                <el-button type="text">分配权限</el-button>
                <el-button type="text" @click="edit(row)">修改</el-button>
                <el-button type="text" @click="del(row)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-tabs>
      <div class="page">
        <el-pagination
          :current-page.sync="page.page"
          :page-sizes="[1, 3, 5, 10, 100, 200]"
          :page-size="page.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 弹框组件 -->
    <Add ref="add" v-model="show" @getData="getData" />
  </div>
</template>

<script>
import { sysRole, sysRoleDelete } from '@/api/settin.js'
import cookidjs from 'js-cookie'
import Add from './components/add.vue'
export default {
  components: {
    Add
  },
  data() {
    return {
      tableData: [],
      show: false,
      page: {
        page: 1,
        pagesize: +cookidjs.get('setting') || 2,
        total: 100
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysRole(this.page)
      this.tableData = res.data.rows
      this.page.total = res.data.total
      // console.log(res)
    },
    handleSizeChange(size) {
      this.page.pagesize = size
      this.page.page = 1
      cookidjs.set('setting', size)
      this.getData()
    },
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
    },
    // 点击确定
    addClick() {
      this.show = true
      this.$refs.add.mode = 'add'
    },
    // 点击修改
    edit(row) {
      this.show = true
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
      this.$refs.add.mode = 'edit'
    },
    async del(row) {
      await this.$confirm('确定要删除吗?', '温馨提示')
      await sysRoleDelete(row.id)
      this.$message.success('删除成功')
      this.page.page = 1
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>
.settin {
  padding: 40px;
  .btn {
    margin: 40px 0 20px;
  }
  .page {
    margin: 10px;
    text-align: right;
  }
  ::v-deep .cell {
    text-align: center;
  }
}
</style>
